package com.hlg.xboot.model.sys;

import com.hlg.xboot.enums.RoleType;
import com.hlg.xboot.model.BaseModel;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by tzw on 2017-06-06.
 */
@DynamicUpdate
@DynamicInsert
@Entity
public class SysRole extends BaseModel {

    /**
     * 角色名称
     */
    @NotEmpty(message = "{SysRole.name.NotEmpty}")
    @Length(max = 20, message = "{SysRole.name.Length}")
    @Column(nullable = false, length = 20)
    private String name;
    /**
     * 角色编码
     */
    @NotEmpty(message = "{SysRole.code.NotEmpty}")
    @Length(max = 20, message = "{SysRole.code.Length}")
    @Column(length = 10, nullable = false)
    private String code;
    /**
     * 描述
     */
    @Column(length = 50)
    private String description;

    /**
     * 角色中的用户
     */
    @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY)
    private Set<SysUser> users;

    /**
     * 角色权限
     */
    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "sys_role_resource",
            joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false)
            , inverseJoinColumns = @JoinColumn(name = "resource_id", referencedColumnName = "id", nullable = false))
    private List<SysResource> resources;

    public SysRole(){

    }

    /**
     * 角色类型
     * 1：系统角色管理员不可更改
     * :2：用户角色可编辑
     */
    private Integer type = RoleType.USER.getValue();

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public List<SysResource> getResources() {
        return resources;
    }

    public void setResources(List<SysResource> resources) {
        this.resources = resources;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<SysUser> getUsers() {
        return users;
    }

    public void setUsers(Set<SysUser> users) {
        this.users = users;
    }

    /**
     * 获取权限code集合
     *
     * @return
     */
    public Set<String> getResourceCode() {
        Set<String> resSet = new HashSet<>();
        for (SysResource resource : this.getResources()) {
            if (StringUtils.isNotEmpty(resource.getCode())) {
                resSet.add(resource.getCode());
            }
        }
        return resSet;
    }
}
